package com.xbjtech.controller;

import java.util.Date;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xbjtech.model.MobileYzm;
import com.xbjtech.service.MobileYzmService;
import com.xbjtech.service.SmsService;
import com.xbjtech.shiro.MobileYzmToken;
import com.xbjtech.util.StringUtil;


@Controller
public class LoginController {
	
	@Autowired
	MobileYzmService mobileYzmService;
	
	@Autowired
	SmsService smsService;

	@GetMapping("/login")
	public String login() {
		return "login";
	}
	
	@PostMapping("/login")
	public String login(String username, String password, Model model) {
		Subject subject = SecurityUtils.getSubject();
		// 去认证
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		token.setRememberMe(true);
		try {
			subject.login(token);
        } catch (UnknownAccountException uae) {
        	model.addAttribute("errorMessage", uae.getMessage());
        	return "login";
        } catch (IncorrectCredentialsException ice) {
        	model.addAttribute("errorMessage", "用户名密码不匹配");
        	return "login";
        } catch (LockedAccountException lae) {
        	model.addAttribute("errorMessage", lae.getMessage());
        	return "login";
        } catch (AuthenticationException ae) {
        	model.addAttribute("errorMessage", ae.getMessage());
        	return "login";
        }
		return "redirect:/main";
		// 成功 重写向到main
		// 失败 转到到/login
	}
	
	@PostMapping("/mobilelogin")
	public String mobileLogin(String mobile, String yzm, Model model) {
		Subject subject = SecurityUtils.getSubject();
		MobileYzmToken token = new MobileYzmToken(mobile, yzm);
		try {
			subject.login(token);
		} catch (AuthenticationException e) {
			model.addAttribute("errorMessage", e.getMessage());
			e.printStackTrace();
			return "login";
		}
		return "redirect:/main";
	}
	
	@RequestMapping("/main")
	public String main() {
		return "main";
	}
	
	@GetMapping("/sendloginyzm")
	@ResponseBody
	public String sendLoginYzm(String mobile) {
		String code = StringUtil.randomString(6);
		smsService.sendLoginYzm(mobile, code);
		
		MobileYzm mobileYzm = new MobileYzm();
		mobileYzm.setMobile(mobile);
		mobileYzm.setCode(code);
		mobileYzm.setCreateTime(new Date());
		mobileYzmService.insert(mobileYzm);
		return "{\"success\":true}";
	}
	
}
